package com.czk.gulimall.order.listener;

import com.czk.gulimall.order.entity.OrderEntity;
import com.czk.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/5/24 21:36
 */
@Component
@Slf4j
@RabbitListener(queues = "order.release.order.queue")
public class OrderRabbitListener {
    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = "order.release.order.queue")
    public void listen(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        try {
            log.info("收到订单过期订单，订单id{}", orderEntity.getOrderSn());
            orderService.closeOrder(orderEntity);
            // 手动确认收到消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
